<!DOCTYPE html>
<html>
<head>
<title>我的投票</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<!-- bootstrap -->
<link href="/css/bootstrap/bootstrap.css" rel="stylesheet" />
<link href="/css/bootstrap/bootstrap-responsive.css" rel="stylesheet" />
<link href="/css/bootstrap/bootstrap-overrides.css" type="text/css"
	rel="stylesheet" />

<!-- libraries -->
<link href="/css/lib/bootstrap-wysihtml5.css" type="text/css"
	rel="stylesheet" />
<link href="/css/lib/uniform.default.css" type="text/css"
	rel="stylesheet" />
<link href="/css/lib/select2.css" type="text/css" rel="stylesheet" />
<link href="/css/lib/bootstrap.datepicker.css" type="text/css"
	rel="stylesheet" />
<link href="/css/lib/font-awesome.css" type="text/css" rel="stylesheet" />

<!-- global styles -->
<link rel="stylesheet" type="text/css" href="/css/layout.css" />
<link rel="stylesheet" type="text/css" href="/css/elements.css" />
<link rel="stylesheet" type="text/css" href="/css/icons.css" />

<!-- this page specific styles -->
<link rel="stylesheet" href="/css/compiled/form-showcase.css"
	type="text/css" media="screen" />

<!-- open sans font -->
<link
	href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800'
	rel='stylesheet' type='text/css' />

<!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

	<!-- navbar -->
	<div class="navbar navbar-inverse">
		<div class="navbar-inner">
			<button type="button" class="btn btn-navbar visible-phone"
				id="menu-toggler">
				<span class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>

			<a class="brand" href="index.html"><img src="/img/logo.png" /></a>

			<ul class="nav pull-right">
				<li class="hidden-phone"><input class="search" type="text" />
				</li>
				<li class="notification-dropdown hidden-phone"><a href="#"
					class="trigger"> <i class="icon-warning-sign"></i> <span
						class="count"></span>
				</a>
					<div class="pop-dialog">
						<div class="pointer right">
							<div class="arrow"></div>
							<div class="arrow_border"></div>
						</div>
						<div class="body">
							<a href="#" class="close-icon"><i class="icon-remove-sign"></i></a>
							<div class="notifications">
								<h3>1条新消息</h3>
								<a href="#" class="item"> <i class="icon-signin"></i> 
									欢迎使用投票系统 <span class="time"><i
										class="icon-time"></i></span>
								</a>
								<div class="footer">
									<a href="#" class="logout">查看所有消息</a>
								</div>
							</div>
						</div>
					</div></li>
				<li class="notification-dropdown hidden-phone"><a href="#"
					class="trigger"> <i class="icon-envelope-alt"></i>
				</a>
					<div class="pop-dialog">
						<div class="pointer right">
							<div class="arrow"></div>
							<div class="arrow_border"></div>
						</div>
						<div class="body">
							<a href="#" class="close-icon"><i class="icon-remove-sign"></i></a>
							<div class="messages">
								<div class="footer">
									<a href="#" class="logout">查看所有信息</a>
								</div>
							</div>
						</div>
					</div></li>
				<li class="dropdown"><a href="#"
					class="dropdown-toggle hidden-phone" data-toggle="dropdown">
						账户信息 <b class="caret"></b>
				</a>
					<ul class="dropdown-menu">
						<li><a href="#">账户详情</a></li>
						<li><a href="#">账户设置</a></li>
						<li><a href="#">账户修改</a></li>
					</ul></li>
				<li class="settings hidden-phone"><a href="#"
					role="button"> <i class="icon-cog"></i>
				</a></li>
				<li class="settings hidden-phone"><a href="/admin/logout"
					role="button"> <i class="icon-share-alt"></i>
				</a></li>
			</ul>
		</div>
	</div>
	<!-- end navbar -->

	<!-- sidebar -->
	<div id="sidebar-nav">
		<ul id="dashboard-menu">
			<li  class="active">
				<div class="pointer">
					<div class="arrow"></div>
					<div class="arrow_border"></div>
				</div> 
				<a href="/vote/statics"> <i class="icon-home"></i> <span>统计首页</span></a>
			</li>
			<li>
				<a href="/vote/create"> <i class="icon-signal"></i> <span>创建投票</span></a>
			</li>
			<li><a class="dropdown-toggle" href="#"> <i
					class="icon-group"></i> <span>用户信息</span> <i
					class="icon-chevron-down"></i>
			</a>
				<ul class="submenu">
					<li><a href="#">用户创建</a></li>
					<li><a href="#">用户信息编辑</a></li>
				</ul></li>
		</ul>
	</div>
	<!-- end sidebar -->


	<!-- main container -->
	<div class="content">

		<div class="container-fluid">
			<div id="pad-wrapper" class="form-page">
				<input type="hidden" id="voteId" value=""></input>
				<input type="hidden" id="vmode" value=""></input>
				<div class="row-fluid form-wrapper">
					<!-- left column -->
					<div class="span8 column">
						<form>
							<div class="field-box">
								<label>投票标题:</label> <span id="title"></span>
							</div>
							<div class="field-box">
								<label>投票主题:</label> <span id="theme"></span>
							</div>
							<div id="userOptions">
							</div>
							<div class="field-box">
                                <label></label>
								<a class="btn-flat primary" onclick="updateUser()">提交</a>
                            </div> 
						</form>
					</div>

					<!-- right column -->
					<div class="span4 column pull-right">
						<form>
							<div class="field-box">
								<label>投票截止日期:</label> <span id="overDate"></span>
							</div>
						</form>
						<form>
						    <div id="user">                    	
                            </div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>

	<div class="field-box" id="radioOpt" style="display:none">
		<div class="field-box" id="checkboxs">
			<label>请选择：</label>
			<div class="span8" id="options"></div>
		</div>
	</div>	
	
	<div id="checkboxOpt"></div>
	
	<label id="cRadio" class="radio" style="display:none">
		<input type="hidden" value="" /> 
		<input style="vertical-align:text-top" type="radio" name="option" id="option" value="1" /> 
		<span style="vertical-align:text-top"> </span> 
		<input style="vertical-align:text-top" class="span2" type="text" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"/>
	</label>

	<div class="field-box" id="cCheckbox" style="display:none">
		<label>请选择：</label> 
		<input type="hidden" value="" /> 
		<input style="vertical-align:text-top" type="checkbox" name="option" id="option" value="1" /> 
		<span style="vertical-align:text-top" ></span> 
		<input class="span2" type="text" style="vertical-align:text-top" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"/>
	</div>

	<div id="cMsg" class="field-box" style="display:none">
		<input type="hidden" value="" /> 
		<label></label> <input class="span8" type="text" />
	</div>

	<!-- end main container -->

	<!-- scripts for this page -->
	<script src="/js/wysihtml5-0.3.0.js"></script>
	<script src="/js/jquery-1.10.2.min.js"></script>
	<script src="/js/bootstrap.min.js"></script>
	<script src="/js/bootstrap-wysihtml5-0.0.2.js"></script>
	<script src="/js/bootstrap.datepicker.js"></script>
	<script src="/js/jquery.uniform.min.js"></script>
	<script src="/js/select2.min.js"></script>
	<script src="/js/theme.js"></script>
	<script src="/js/pokemon.js"></script>

	<!-- call this page plugins -->
	<script type="text/javascript">
	
		var isVoteSave = false;
		
		var voteUserId = null;
	
		function updateUser(){
			
			if(isVoteSave){
				alert('正在保存结果中，请稍侯...');
				return;
			}
			
			var voteUser = {};
			var vmode = $('#vmode').val();
			voteUser.voteId = $('#voteId').val();
			voteUser.id = voteUserId;
			voteUser.voteUserOptionDtos = [];
			voteUser.voteUserMsgDtos = [];
			//处理选项
			for(var i=1;;i++){
				var $option = $('#option'+i);
				if($option.length==0){
					break;
				}
				//单选被选中 或者 多选被选中
				if((vmode==0&&$option.find('input:radio').prop('checked'))||
						(vmode==1&&$option.find('input:checkbox').prop('checked'))){
					voteUser.voteUserOptionDtos[i-1] = {};
					voteUser.voteUserOptionDtos[i-1].voteId = voteUser.voteId;
					voteUser.voteUserOptionDtos[i-1].isSelect = 1;
					voteUser.voteUserOptionDtos[i-1].voteOptionId = $option.find('input:hidden').val();
					if(!($option.find('input:text').val()=="")){
						voteUser.voteUserOptionDtos[i-1].optionNum = $option.find('input:text').val();
					}			
				}
			}
			
			if(voteUser.voteUserOptionDtos.length==0){
				alert('请选择一个选项');
				return;
			}
			
			//处理输入信息
			for(var i=1;;i++){
				var $msg = $('#msg'+i);
				if($msg.length==0){
					break;
				}
				var memo = $msg.find('label').html();
				if(!$msg.find('input:text').val()){
					alert(memo+'不能为空');
					return;
				}
				voteUser.voteUserMsgDtos[i-1] = {};
				voteUser.voteUserMsgDtos[i-1].voteId = voteUser.voteId;
				voteUser.voteUserMsgDtos[i-1].voteMsgId = $msg.find('input:hidden').val();
				voteUser.voteUserMsgDtos[i-1].msgValue = $msg.find('input:text').val();
			}
			
			var jdata = JSON.stringify(voteUser);
			isVoteSave = true;
			postContent("/vote/updateUser",jdata,true,function(data){
				if(data!=null){
					alert('修改成功!');
				}
				isVoteSave = false;
			});
		}
		
        $(function () {

            // select2 plugin for select elements
            $(".select2").select2({
                placeholder: "Select a State"
            });

            // datepicker plugin
            $('.datepicker').datepicker().on('changeDate', function (ev) {
                $(this).datepicker('hide');
            });

            // wysihtml5 plugin on textarea
            $(".wysihtml5").wysihtml5({
                "font-styles": false
            });
            
            
            var voteId = getUrlParam('voteId');
            voteUserId = getUrlParam('voteUserId');
            if(voteUserId==null){
            	alert('无法获取用户投票信息!');
            }
			getContent("/vote/get",'voteId='+voteId,true,function(data){
				if(data!=null){
					$('#voteId').val(data.id);
					$('#vmode').val(data.vmode);
					$('#title').html(data.title);
					$('#theme').html(data.theme);
					$('#overDate').html(data.overDateStr);
					$cOption = null; 
					$show = null; //全部展示的div
					$append = null;
					if(data.vmode==0){
						$cOption = $('#cRadio').clone(false);
						$show= $('#radioOpt').clone(false);
						$append = $show.find('#options');
					}else{
						$cOption = $('#cCheckbox').clone(false);
						$show= $('#checkboxOpt').clone(false);
						$append = $show;
					}
					$show.attr("style", "display:block;");
					for(var i=0;i<data.voteOptionDtos.length;i++){
						var optionData = data.voteOptionDtos[i];
						$curOption = $cOption.clone(false);
						$curOption.attr("id", "option" + (i + 1));
						$curOption.attr("style", "display:block;");
						$curOption.find('span').html(optionData.optionstr);
						$curOption.find('input:hidden').val(optionData.id);
						$curOption.find('input:text').val('');
						$append.append($curOption);
						if(i>0){//这是可选框用的
							$curOption.find('label').html('');
						}
						if (optionData.isinput == 0) {
							$curOption.find('input:text').hide();
						}
					}
					$('#userOptions').append($show);

					for (var i = 0; i < data.voteMsgDtos.length; i++) {
						var msgData = data.voteMsgDtos[i];
						$curMsg = $('#cMsg').clone(false);
						$curMsg.find('input:hidden').val(msgData.id);
						$curMsg.find('input:text').val('');
						$curMsg.attr("id", "msg" + (i + 1));
						$curMsg.attr("style", "display:block;");
						$curMsg.children('label').html(msgData.msg);
						$('#user').append($curMsg);
					}
					
					var getUserParam = {};
					getUserParam.voteId = voteId;
					getUserParam.voteUserId = voteUserId;
					getContent("/vote/getUser",getUserParam,true,function(data){
						var msgs = data.voteUserMsgDtos;
						if(msgs.length>0){
							for(num in msgs){
								var msgValue = msgs[num].msgValue;
								var msgId = msgs[num].voteMsgId;
								$('input:hidden[value='+msgId+']').parent('[id^=msg]').find('input:text').val(msgValue)
							}
						}
						var options = data.voteUserOptionDtos;
						if(options.length>0){
							for(num in options){
								var optionId = options[num].voteOptionId;
								var voteUserId = options[num].voteUserId;
								var optionNum = options[num].optionNum;
								var isSelect = options[num].isSelect;
								if(isSelect==1){
									var $option = $('input:hidden[value='+optionId+']').parent('[id^=option]');
									$option.find('input:text').val(optionNum);
									$option.find('#option').prop('checked',true)
								}
							}
						}
					});
				}
			});

		});
	</script>
	<div style="display: none">
		<script src='http://v7.cnzz.com/stat.php?id=155540&web_id=155540'
			language='JavaScript' charset='gb2312'></script>
	</div>
</body>